import { instance } from '@/api/shared'
import type { Resp } from "@/api/shared/type.ts";

export type ClusterConfig = {
    name: string,
    cluster_type: string,
    kube_config_content: string
}

export async function AddClusterConfig(data: ClusterConfig): Promise<Resp> {
    try {
        const res = await instance.post('/cluster/add', data);
        return res.data;  // 直接返回 data
    } catch (error) {
        console.error(error);
        throw error;  // 可以选择重新抛出错误，或者返回默认值
    }
}

export async function GetAllClusterConfig(type: string): Promise<Resp> {
    try {
        const res = await instance.get(`/cluster/all?type=${type}`);
        return res.data;
    } catch (error) {
        console.error(error);
        throw error;
    }
}

// --- Cluster Overview APIs ---

/**
 * 获取集群基本信息
 * @param clusterId 集群ID
 */
export async function GetClusterInfo(clusterId: string): Promise<Resp> {
    try {
        // 注意：这里的URL是示例，请根据您的后端API进行调整
        const res = await instance.get(`/cluster/get/${clusterId}`);
        return res.data;
    } catch (error) {
        console.error(`Failed to get cluster info for ${clusterId}:`, error);
        throw error;
    }
}

/**
 * 获取集群资源使用情况
 * @param clusterId 集群ID
 */
export async function GetClusterResourceUsage(clusterId: string): Promise<Resp> {
    try {
        const res = await instance.get(`/cluster/resources/${clusterId}`);
        return res.data;
    } catch (error) {
        console.error(`Failed to get cluster resource usage for ${clusterId}:`, error);
        throw error;
    }
}

/**
 * 获取集群节点列表
 * @param clusterId 集群ID
 */
export async function GetClusterNodes(clusterId: string): Promise<Resp> {
    try {
        const res = await instance.get(`/cluster/${clusterId}/nodes`);
        return res.data;
    } catch (error) {
        console.error(`Failed to get cluster nodes for ${clusterId}:`, error);
        throw error;
    }
}

/**
 * 获取集群命名空间列表
 * @param clusterId 集群ID
 */
export async function GetClusterNamespaces(clusterId: string): Promise<Resp> {
    try {
        const res = await instance.get(`/cluster/${clusterId}/namespaces`);
        return res.data;
    } catch (error) {
        console.error(`Failed to get cluster namespaces for ${clusterId}:`, error);
        throw error;
    }
}

export async function GetClusterPodsByNamespace(clusterId: string, namespace: string): Promise<Resp> {
    try {
        const res = await instance.get(`/cluster/pod/list/${clusterId}/${namespace}`);
        return res.data;
    } catch (error) {
        console.error(`Failed to get pods for cluster ${clusterId} in namespace ${namespace}:`, error);
        throw error;
    }
}

export async function DeletePod(clusterId: number, namespace: string, podName: string): Promise<Resp> {
    try {
        const res = await instance.post(`/cluster/pod/delete`, {
            podName,
            clusterId,
            namespace
        });
        return res.data;
    } catch (error) {
        console.error(`Failed to delete pod ${podName} in cluster ${clusterId} and namespace ${namespace}:`, error);
        throw error;
    }
}

export async function CreatePodByYaml(clusterId: number, yText: string): Promise<Resp> {
    try {
        const res = await instance.post('/cluster/pod/createByYaml', {
            clusterId,
            yaml: yText
        });
        return res.data;
    } catch (error) {
        console.error(`Failed to create pod from YAML:`, error);
        throw error;
    }
}

export async function GetClusterDeploymentsByNamespace(clusterId: string, namespace: string): Promise<Resp> {
    try {
        const res = await instance.get(`/cluster/deployment/list/${clusterId}/${namespace}`);
        return res.data;
    } catch (error) {
        console.error(`Failed to get deployments for cluster ${clusterId} in namespace ${namespace}:`, error);
        throw error;
    }
}

export async function DeleteDeployment(clusterId: number, namespace: string, deploymentName: string): Promise<Resp> {
    try {
        const res = await instance.post(`/cluster/deployment/delete`, {
            deploymentName,
            clusterId,
            namespace
        });
        return res.data;
    } catch (error) {
        console.error(`Failed to delete deployment ${deploymentName} in cluster ${clusterId} and namespace ${namespace}:`, error);
        throw error;
    }
}

export async function CreateDeploymentByYaml(clusterId: number, yText: string): Promise<Resp> {
    try {
        const res = await instance.post('/cluster/deployment/createByYaml', {
            clusterId,
            yaml: yText
        });
        return res.data;
    } catch (error) {
        console.error(`Failed to create deployment from YAML:`, error);
        throw error;
    }
}

/**
 * 获取节点之间的网络延迟数据
 * @param clusterId 集群ID
 * @param nodeId1 节点1的ID
 * @param nodeId2 节点2的ID
 * @param startTime 起始时间 (RFC3339格式字符串)
 * @param endTime 结束时间 (RFC3339格式字符串)
 */
export async function GetNodeNetworkLatency(
    clusterId: string,
    nodeId1: string,
    nodeId2: string,
    startTime: string,
    endTime: string
): Promise<Resp> {
    try {
        const res = await instance.get(`/cluster/latency/pair/${clusterId}?node1=${nodeId1}&node2=${nodeId2}&start_time=${encodeURIComponent(startTime)}&end_time=${encodeURIComponent(endTime)}`);
        return res.data;
    } catch (error) {
        console.error(`Failed to get network latency between ${nodeId1} and ${nodeId2}:`, error);
        throw error;
    }
}
